package com.newly.center.sys.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.newly.api.sys.feign.DictClient;
import com.newly.center.sys.service.dict.api.IDictService;
import com.newly.center.sys.service.tenant.api.ITenantService;
import com.newly.common.base.constants.CodeConstant;
import com.newly.common.base.constants.ServerUriConstant;
import com.newly.common.base.entity.PageUtils;
import com.newly.common.base.entity.R;
import com.newly.common.base.entity.auth.ICurrentTenant;
import com.newly.common.base.entity.sys.dict.dto.CreateDictDto;
import com.newly.common.base.entity.sys.dict.dto.DictTypeDto;
import com.newly.common.base.entity.sys.dict.dto.UpdDictDto;
import com.newly.common.base.entity.sys.dict.po.DictPo;
import com.newly.common.base.entity.sys.dict.po.DictTypePo;
import com.newly.common.base.entity.sys.dict.vo.DictTypeVo;
import com.newly.common.base.entity.sys.dict.vo.DictVo;
import com.newly.common.base.entity.sys.dict.vo.TenantDictVo;
import com.newly.common.base.entity.sys.dict.vo.UpdDictVo;
import com.newly.common.core.utils.ExcelUtil;
import com.newly.common.core.utils.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author gjm guan
 * @date 2021/11/9 16:27
 * @desc 字典表api
 */
@RestController
@RequestMapping(ServerUriConstant.NEWLY_SYS_DICT)
public class DictApi implements DictClient {

    @Autowired
    private IDictService dictService;
    
    @Autowired
    private ITenantService tenantService;

    @Autowired
    private RequestUtils requestUtils;

    @Override
    public void exportDict(String keyword, String startTime, String endTime, HttpServletResponse response) {
        List<DictPo> pos = dictService.listAllDict(keyword, startTime, endTime);
        List<DictVo> list = pos.stream().map(po -> {
            DictVo vo = BeanUtil.copyProperties(po, DictVo.class, "createTime");
            vo.setCreateTime(
                    DateUtil.format(po.getCreateTime(), "yyyy-MM-dd HH:mm:ss")
            );
            return vo;
        }).collect(Collectors.toList());
        ExcelUtil.export(response, "参数配置表", null, list, DictVo.class);
    }

    @Override
    public R keyDuplicate(String code) {
        return dictService.keyDuplicate(code) ? R.fail("参数键名已存在") : R.ok();
    }

    @Override
    public R queryDictById(Integer id) {
        UpdDictVo updDictVo = dictService.queryDictById(id);
        return R.ok(updDictVo);
    }

    @Override
    public R queryTenantOfCreateDict() {
        List<TenantDictVo> tenantDictVos = dictService.queryTenantOfCreateDict();
        return R.ok(CodeConstant.SUCCESS, tenantDictVos, "查询字典创建人成功");
    }

    @Override
    public R createDict(CreateDictDto dictDto) {
        ICurrentTenant currentTenant = requestUtils.getCurrentTenant();
        boolean result = dictService.createDict(currentTenant, dictDto);
        return R.ok(CodeConstant.SUCCESS, result, "创建字典成功");
    }

    @Override
    public R listDict(String keyword, Integer offset, Integer limit, String startTime, String endTime) {
        PageUtils dictPage = dictService.listDict(keyword, offset, limit, startTime, endTime);
        if (!ObjectUtil.isEmpty(dictPage) && !ObjectUtil.isEmpty(dictPage.getList())) {
            List<DictPo> dictPos = dictPage.getList();
            List<DictVo> dictVos = dictPos.stream().map(dictPo -> {
                DictVo dictVo = BeanUtil.copyProperties(dictPo, DictVo.class, "createTime");
                dictVo.setCreateTime(
                        DateUtil.format(dictPo.getCreateTime(), "yyyy-MM-dd HH:mm:ss")
                );
                return dictVo;
//                dictVo.setTenantDict(tenantService.getTempTenant(dictPo.getCreateTenantId()));
            }).sorted((v1, v2) -> v1.getSort() - v2.getSort()).collect(Collectors.toList());
            dictPage.setList(dictVos);
        }

        return R.ok(CodeConstant.SUCCESS, dictPage, "查询字典列表成功");
    }

    @Override
    public R modifyDict(UpdDictDto dictDto) {
        boolean result = dictService.modifyDict(dictDto);
        return R.ok(CodeConstant.SUCCESS, result, "修改字典成功");
    }

    @Override
    public R removeDict(List<String> dictIds) {
        boolean result = dictService.removeDict(dictIds);
        return R.ok(CodeConstant.SUCCESS, result, "删除字典成功");
    }

    @Override
    public R listDictType(String keyword, Integer offset, Integer limit, String startTime, String endTime) {
        PageUtils dictTypePage = dictService.listDictType(keyword, offset, limit, startTime, endTime);
        if (!ObjectUtil.isEmpty(dictTypePage) && !ObjectUtil.isEmpty(dictTypePage.getList())) {
            List<DictTypePo> dictTypePos = dictTypePage.getList();
            List<DictTypeVo> dictTypeVos = dictTypePos.stream().map(dictTypePo ->
                    BeanUtil.copyProperties(dictTypePo, DictTypeVo.class)
            ).collect(Collectors.toList());
            dictTypePage.setList(dictTypeVos);
        }
        return R.ok(CodeConstant.SUCCESS, dictTypePage, "查询字典类型成功");
    }

    @Override
    public R queryDictType() {
        List<DictTypePo> dictTypePos = dictService.queryDictType();
        List<DictTypeVo> dictTypeVos = null;
        if (!ObjectUtil.isEmpty(dictTypePos)) {
            dictTypeVos = BeanUtil.copyToList(dictTypePos, DictTypeVo.class);
        }
        return R.ok(CodeConstant.SUCCESS, dictTypeVos, "查询字典类型成功");
    }

    @Override
    public R removeDictType(List<String> dictTypeIds) {
        boolean result = dictService.removeDictType(dictTypeIds);
        return R.ok(CodeConstant.SUCCESS, result, "删除字典类型成功");
    }

    @Override
    public R createDictType(DictTypeDto dictTypeDto) {
        boolean result = dictService.createDictType(dictTypeDto);
        return R.ok(CodeConstant.SUCCESS, result, "创建字典类型成功");
    }

    @Override
    public R modifyDictType(DictTypeDto dictTypeDto) {
        boolean result = dictService.modifyDictType(dictTypeDto);
        return R.ok(CodeConstant.SUCCESS, result, "修改字典类型成功");
    }

    @Override
    public R refreshCache() {
        this.dictService.refreshCache();
        return R.ok();
    }
}
